<?php

class M_teachers Extends MY_Model
{
	private $tuid; // teacher user id
	
	public function __construct()
	{
		parent::__construct();
		$this->tuid = $this->get_employee_id();
	}
	
	public function get_employee_id()
	{ 
		$sql = "SELECT e.emp_id
				FROM employees e
				LEFT JOIN users u ON u.id = e.id
				WHERE e.id = ?
				LIMIT 1";
				
		$query = $this->db->query($sql,array($this->c_user->userid));
		if($query->num_rows() >=1)
		{
			return $query->row()->emp_id;
		}else{
			return FALSE;
		}
	}
	
	public function get_classes()
	{
		$classes = $this->get_class_section_room();
		if($classes !== FALSE)
		{
			foreach($classes as $k => $v)
			{
				$processed[$k]['section_name'] = $v->section_name;
				$processed[$k]['section_id'] = $v->id;
				$processed[$k]['room_desc'] = $v->description;
				$processed[$k]['room_name'] = $v->name;
				$processed[$k]['section_studs'] = $this->get_student_for_section($v->id);
				$processed[$k]['class_sched'] = $this->_get_class_schedules($v->id);
			}
			return $processed;
		}else{
			return FALSE;
		}
	}
	
	private function get_class_section_room()
	{
		$sql = 'SELECT s.section_name,
					   s.id,
					   r.description,
					   r.name
				FROM sections s
				LEFT JOIN rooms r ON r.section_id = s.id
				WHERE s.adviser_id = ?';
				
		$query = $this->db->query($sql,array($this->tuid));
		return $query->num_rows() >=1 ? $query->result() : FALSE;
	}
	
	private function get_student_for_section($section_id)
	{
		$sql = 'SELECT e.e_id as eid,
					   p.profile_id as pid,
					   concat(p.lastname," ",p.firstname," , ",p.middlename) as fullname,
					   p.age,
					   p.dob,
					   gender
				FROM enrollments e
				LEFT JOIN profiles p ON p.profile_id = e.e_profile_id
				WHERE e.e_section_id = ? 
				order by gender';
				
		$query = $this->db->query($sql,array($section_id));
		return $query->num_rows() >=1 ? $query->result() : FALSE;
	}
	
	private function _get_class_schedules($section_id)
	{
		$sql = 'SELECT sched.time_from,
					   sched.time_to,
					   sched.day,
					   su.subj_code,
					   su.subj_desc,
					   concat(e.first_name," ",e.middle_name," ",e.last_name) as teacher
				FROM schedule sched
				LEFT JOIN sections s ON sched.section_id = s.id
				LEFT JOIN subjects su ON su.subj_id = sched.subject_id
				LEFT JOIN employees e ON e.emp_id = sched.teacher_id
				WHERE s.id = ?';
		$query = $this->db->query($sql,array($section_id));
		return $query->num_rows() >= 1 ? $query->result() : FALSE;
	}
	
	
	public function get_schedules()
	{
		$sched = $this->_get_schedules();
		if($sched)
		{
			foreach($sched as $k => $v)
			{
				$time = $v->time_from.' - '.$v->time_to;
				
				$processed[$k]['time'] = $time;
				$processed[$k]['day'] = ucwords($v->day);
				$processed[$k]['section_name'] = ucwords($v->section_name);
				$processed[$k]['room_name'] = ucwords($v->name);
				$processed[$k]['room_desc'] = ucwords($v->description);
				$processed[$k]['level_desc'] = ucwords($v->level_desc);
				$processed[$k]['adviser'] = ucwords($v->adviser);
				$processed[$k]['subj'] = ucwords($v->subj_desc);
				$processed[$k]['studs'] = $this->get_student_for_section($v->id);
			}
			return $processed;
		}else
		{
			return FALSE;
		}
	}
	
	
	private function _get_schedules()
	{
		$sql = 'SELECT sched.time_from,
					   sched.time_to,
					   sched.day,
					   s.section_name,
					   s.id,
					   r.name,
					   r.description,
					   l.level_desc,
					   concat(e.first_name," ",e.middle_name," ",e.last_name) as adviser,
					   su.subj_desc
				FROM schedule sched
				LEFT JOIN sections s ON sched.section_id = s.id
				LEFT JOIN rooms r ON r.section_id = s.id
				LEFT JOIN levels l ON l.level_id = s.grade_level_id
				LEFT JOIN employees e ON e.emp_id = s.adviser_id
				LEFT JOIN subjects su ON su.subj_id = sched.subject_id
				WHERE teacher_id = ?
				AND s.adviser_id != ?';
		$query = $this->db->query($sql,array($this->tuid,$this->tuid));
		return $query->num_rows() >=1 ? $query->result() : FALSE;
	}
}